<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		<myheader class="my-header" :leftType="2" :noLeft="false" :showTitle="store.storeName" :textLeft="textLeft"></myheader>
		<!-- #endif -->
		<view class="content">
			<scroll-view :scroll-y="true" :scrollbar="false" class="content-left"  :style="{ top: `${headerHight + 2}px` }">
				<view class="content-left-item" :class="index == activeIndex?'content-left-active':''" @tap="itemclick(item, index)" v-for="(item, index) in serviceList" :key="index">{{item.menuName}}</view>
			</scroll-view>
			<scroll-view :scroll-y="true" :scrollbar="false" class="content-right">
				<view class="card" v-if="serviceList[activeIndex] && serviceList[activeIndex].menuChildInfoList">
					<view class="card-item" v-for="(item,index) in serviceList[activeIndex].menuChildInfoList" :key="index">
						<image :src="item.logo" class="card-item-img" mode=""></image>
						<view class="card-item-right">
							<view class="card-item-txt">{{item.name}}</view>
							<view v-if="hasServiceIdList.indexOf(item.id)>0 && nickname" style="display: flex;align-items: center;" v-else >
								<view class="card-item-dingyue card-item-border">
									看饮食推荐
								</view>
								<view  @tap="cancelSub(item)" style="color:#E74C3C;font-weight: 400;font-size: 14px;margin-left:4px">取消订阅</view>
							</view>
							<view v-else class="card-item-btn card-item-dingyue" @click.stop="dingyue(item)">
								免费订阅
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="tabbar-wrap">
				<block >
					<view @click="onFirst" class="tab-item">
						<image class="icon" :src="store.storeLogo"></image>
					</view>
				</block>
				<block v-for="(item, index) in list" :key="index">
					<view @click="onTab(item)" v-show="active !== index" class="tab-item">
						<image class="icon" :class="{ 'mid-button': item.midButton }" :src="item.iconPath"></image>
						<text  class="txt" :style="{ color: color }">{{ item.text }}</text>
					</view>
					<view @click="onTab(item)" v-show="active === index" class="tab-item">
						<image class="icon" :class="{ 'mid-button': item.midButton }" :src="item.selectedIconPath"></image>
						<text class="txt" :style="{ color: selectedColor }">{{ item.text }}</text>
					</view>
				</block>
			</view>
	</view>
</template>

<script>
import {
	mapGetters
} from "vuex";
	import myheader from '../../components/header/index.vue';
	export default {
		components: {
			myheader,
		},
		data() {
			return {
				textLeft: true,
				active: 0,
				serviceList: [],
				activeIndex: 0,
				headerHight: 80,
				list: [
					// {
					// 	"pagePath": "/pages/index/index",
					// 	"text": "吃巧",
					// 	"iconPath": "/static/tabBar/first.svg", // 默认图片
					// 	"selectedIconPath": "/static/tabBar/firstSelect.svg" // 选中图片
					// },
					{
						"pagePath": "/pages/shop/service",
						"text": "服务",
						"iconPath": "/static/tabBar/second.svg", // 默认图片
						"selectedIconPath": "/static/tabBar/secondSelect.svg" // 选中图片
					},
					{
						"pagePath": "/pages/shop/sub",
						"text": "已订",
						"iconPath": "/static/tabBar/third.svg", // 默认图片
						"selectedIconPath": "/static/tabBar/thirdSelect.svg" // 选中图片
					},
					{
						"pagePath": "/pages/shop/about",
						"text": "关于",
						"iconPath": "/static/tabBar/fourth.svg", // 默认图片
						"selectedIconPath": "/static/tabBar/fourthSelect.svg" // 选中图片
					}
				],
				hasServiceIdList: []
			}
		},
		computed: {
			...mapGetters(["store","nickname"]),
		},
		onShow() {
			this.getMenu()
			if(this.nickname) {
				this.gethasServiceIdList()
			}
		},
		beforeMount() {
			let selectQuery = uni.createSelectorQuery().in(this);
			selectQuery.selectAll('.my-header').boundingClientRect(rect => {
				this.headerHight = rect[0].height;
			}).exec()
			this.$forceUpdate();
		},
		methods: {
			async getMenu() {
				let info = await this.$H.getToB("portal/menu",{
					storeId: this.store.storeId
				})
				console.log(info, 'info-----')
				if (info.code ==0) {
					this.serviceList = info.data
				} 
			},
			onFirst() {
				uni.reLaunch({
					url: '/pages/shop/shop?shopId=' + this.store.storeId
				})
			},
			onTab(item) {
				uni.navigateTo({
					url: item.pagePath
				})
			},
			itemclick(item, index) {
				this.activeIndex = index
			},
			gethasServiceIdList() {
				this.$H.post("portal/sub/searchIds").then((res => {
					if (res.code == 0) {
						this.hasServiceIdList = res.data;
						this.$forceUpdate();
					}
				}));
			},
			cancelSub(service) {
				let that = this
				uni.showModal({
					content: '确定要取消订阅吗',
					confirmColor: "#E02828",
					success: function (res) {
						if (res.confirm) {
							uni.showLoading({
								title: '取消中',
								mask: true
							})
							that.trueCancel(service)
						} else if (res.cancel) {
							return false;
						}
					},
				});
				
			},
			trueCancel(service) {
				this.$H.delete("portal/sub/cancelSub?subId=" + service.id).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						uni.showToast({
							title: "取消成功",
							icon: "none"
						})
						this.gethasServiceIdList();
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			},
			dingyue(service) {
				if (!this.nickname) {
					uni.navigateTo({
						url: "/pages/login/login"
					})
				} else {
			
					let postData = {
						"serviceId": service.id,
						storeId:this.store.storeId
					}
					uni.showLoading({
						title: '正在订阅...',
						mask: true
					});
					this.$H.post("portal/sub/sub?serviceId=" + service.id + '&storeId='+postData.storeId, postData).then(res => {
						uni.hideLoading();
						if (res.code == 0) {
							uni.showToast({
								title: "订阅成功",
								icon: "none"
							})
							this.gethasServiceIdList();
							this.subCount++
						} else {
							uni.showToast({
								title: res.msg,
								icon: "none"
							})
						}
					});
				}
			},
			
		}
	}
</script>

<style lang="scss" scoped>
.tabbar-wrap {
	    position: fixed;
	    bottom: 0;
	    width: 100%;
	    z-index: 90;
	    background-color: #fff;
	    padding: 20px 0px 20px 0px;
		padding-bottom: calc(constant(safe-area-inset-bottom));
		padding-bottom: calc(env(safe-area-inset-bottom));
	    display: flex;
	    align-items: center;
		border-top: 1px solid #f4f6f7;
		.tab-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			flex: 1;
			height: 100%;
			border-radius: 8px;
			position: relative;
	
			.icon {
				width: 24px;
				height: 24px;
				border-radius: 50%;
			}
	
			.txt {
				margin-top: 3px;
				font-size: 12px;
			}
	
			.number {
				background-color: #FA3534;
				color: #fff;
				border-radius: 50%;
				position: absolute;
				right: 25rpx;
				top: 5px;
				display: inline-block;
				width: 20px;
				height: 20px;
				font-size: 10px;
				display: flex;
				justify-content: center;
				align-items: center;
			}
	
			.mid-button {
				width: 43px;
				height: 43px;
			}
		}
	
		.tab-item[hidden] {
			display: none !important;
		}
	}
	.content {
		display: flex;
		border-top: 2px solid #eaecef;
		&-left {
			width: 80px;
			height: 100vh;
			background: #fff;
			position: fixed;
			left: 0;
			&-item {
				height: 56px;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 14px;
				font-weight: 700;
				color: rgba(130, 146, 162, 1)
			}
			&-active {
				background: #eaecef;
				color: #1ABC9C;
			}
		}
		&-right{
			margin-left: 80px;
			padding-bottom: 50px;
			height: 100vh
		}
	}
	.card {
		padding: 10px 10px 50px;
		&-item {
			display: flex;
			border-radius: 5px;
			margin-bottom: 10px;
			height: 96px;
			&-right{
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				padding: 6px 10px;
				background: #fff;
				border-bottom-right-radius: 5px;
				border-top-right-radius: 5px;
				flex:1;
			}
			&-img {
				border-top-left-radius: 5px;
				border-bottom-left-radius: 5px;
				width: 96px;
				height: 96px;
				background: rgba(203, 209, 216, 1);
			}
			&-txt {
				max-width: 160px;
				color: #34495E;
				font-size: 16px;
				font-weight: 700;
				white-space: normal;
				word-break: break-all;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
				margin-bottom: 10px;
			}
			&-dingyue {
				display: inline-block;
				font-size: 14px;
				font-weight: 400;
				border-radius: 15px;
				padding: 6px 5px;
				margin-right: 10px;
			}
			&-border {
				border: 1px solid #1ABC9C;
				color: #1ABC9C;
				background: #fff;
			}
			&-btn {
				color: #fff;
				background-color: #1ABC9C;
				font-size: 14px;
				font-weight: 700;
				border: none;
				width: 66px;
				text-align: center;
			}
		}
	}
</style>